package com.yunxi.service.localService.impl;

import cn.jiguang.common.ClientConfig;
import cn.jiguang.common.resp.APIConnectionException;
import cn.jiguang.common.resp.APIRequestException;
import cn.jpush.api.JPushClient;
import cn.jpush.api.push.PushResult;
import cn.jpush.api.push.model.Message;
import cn.jpush.api.push.model.Platform;
import cn.jpush.api.push.model.PushPayload;
import cn.jpush.api.push.model.audience.Audience;
import cn.jpush.api.push.model.audience.AudienceTarget;
import cn.jpush.api.push.model.notification.Notification;
import com.yunxi.service.localService.JpushForBizService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * Created by caoYong on 2019/08/24.
 */
@Component
@Service
public class JpushForBizServiceImpl implements JpushForBizService {

    private static final Logger logger = LoggerFactory.getLogger(JpushForBizServiceImpl.class);
    public static String appKey = "03e6e91d3eda866159962343";
    public static String masterSecret = "024a9e6b697b28b4fa824ca6";

    public static String pdaKey = "e2a5d63638743730efe014fd";
    public static String pdaMasterSecret = "6f88bfa588f48323ffc13558";

    @Override
    public int send(String userCode,String roleCode,String content ){
        JPushClient jpushClient = new JPushClient(masterSecret, appKey, null, ClientConfig.getInstance());

        PushPayload payload = buildPushObject_all_alias_tag_alert(userCode,roleCode,content);

        try {
            PushResult result = jpushClient.sendPush(payload);
            logger.debug("===========================消息体"+payload.toString()+"=============");
            return result.getResponseCode();
        } catch (APIConnectionException e) {

        } catch (APIRequestException e) {

        }
        return 1000;
    }

    @Override
    public int send(List<String> salesConsultantNos, List<String> receiveRoleCodes, String content) {

        JPushClient jpushClient = new JPushClient(masterSecret, appKey, null, ClientConfig.getInstance());

        PushPayload payload = buildPushObject_all_alias_tag_alert(salesConsultantNos,receiveRoleCodes,content);

        try {
            PushResult result = jpushClient.sendPush(payload);
            logger.debug("===========================消息体"+payload.toString()+"=============");
            return result.getResponseCode();
        } catch (APIConnectionException e) {

        } catch (APIRequestException e) {

        }
        return 1000;
    }

    /**
     * 推送消息到PDA
     * @param userCode
     * @param roleCode
     * @param content
     * @return
     */
    @Override
    public int sendToPda(String userCode,String roleCode,String content ){
        JPushClient jpushClient = new JPushClient(pdaMasterSecret, pdaKey, null, ClientConfig.getInstance());

        PushPayload payload = buildPushObject_all_alias_alert(userCode,roleCode,content);

        try {
            PushResult result = jpushClient.sendPush(payload);
            logger.debug("===========================消息体"+payload.toString()+"=============");
            return result.getResponseCode();
        } catch (APIConnectionException e) {

        } catch (APIRequestException e) {

        }
        return 1000;
    }

    private PushPayload buildPushObject_all_alias_tag_alert(String userCode, String roleCode, String content) {
        return PushPayload.newBuilder()
                .setPlatform(Platform.android_ios())
                .setAudience(Audience.newBuilder()
                        .addAudienceTarget(AudienceTarget.tag(roleCode))
                        .addAudienceTarget(AudienceTarget.alias(userCode))
                        .build())
                .setNotification(Notification.alert(content))
                .build();
    }

    private PushPayload buildPushObject_all_alias_tag_alert(List<String> salesConsultantNos, List<String> receiveRoleCodes, String content) {
        return PushPayload.newBuilder()
                .setPlatform(Platform.android_ios())
                .setAudience(Audience.newBuilder()
                        .addAudienceTarget(AudienceTarget.tag(salesConsultantNos))
                        .addAudienceTarget(AudienceTarget.alias(receiveRoleCodes))
                        .build())
                .setNotification(Notification.alert(content))
                .build();
    }

    private PushPayload buildPushObject_all_alias_alert(String userCode, String roleCode, String content) {
        return PushPayload.newBuilder()
                .setPlatform(Platform.android_ios())
                .setAudience(Audience.newBuilder()
                        .addAudienceTarget(AudienceTarget.alias(userCode))
                        .build())
                .setNotification(Notification.alert(content))
                .build();
    }

}
